<template>
  <div>
    <div class="search">
      <el-form :inline="true" :model="formInline" class="demo-form-inline">
        <el-form-item label="申请人">
          <el-input v-model="formInline.user" placeholder="申请人" clearable />
        </el-form-item>
        <el-form-item label="申请时间">
          <el-date-picker
            v-model="value1"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div>
      <el-table
        :data="tableData"
        style="width: 100%"
        :fit="true"
      >
        <el-table-column
          label="申请时间"
          width="300"
          align="center"
        >
          <template slot-scope="scope">
            <i class="el-icon-time" />
            <span style="margin-left: 10px">{{ scope.row.createTime }}</span>
          </template>
        </el-table-column>
        <el-table-column
          label="申请人"
          width="180px"
          align="center"
        >
          <template slot-scope="scope">
            <div slot="reference" class="name-wrapper">
              <el-tag size="medium" effect="dark" type="success">{{ scope.row.username }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <el-table-column
          label="申请文件"
          width="600"
          align="center"
        >
          <template slot-scope="scope">
            <a v-for="(file, index) in scope.row.urls" :key="index" style="display:block;margin-top:20px;" @click="downloadFile(file)">{{ file }}</a>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handlePass(scope.row)"
            >通过</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleReject(scope.row)"
            >驳回</el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <div class="paging">
      <el-pagination
        background
        layout="prev, pager, next"
        :total="total"
        :current-page="currentPage"
      />
    </div>
  </div>
</template>

<script>
import { getUnAuditList, changeStatus } from '@/api/application'
export default {
  data() {
    return {
      imgs: ['.jpg', '.png', '.gif', '.webp', '.jpeg'],
      formInline: {
        user: '',
        region: ''
      },
      value1: [],
      tableData: [],
      total: 1,
      currentPage: 1
    }
  },
  mounted() {
    this.getUnAuditList()
  },
  methods: {
    getUnAuditList() {
      var startTime
      var endTime
      if (this.value1 === null) {
        startTime = null
        endTime = null
      } else {
        startTime = typeof this.value1[0] === 'undefined' ? null : this.value1[0]
        endTime = typeof this.value1[1] === 'undefined' ? null : this.value1[1]
      }
      getUnAuditList({ username: this.formInline.user,
        startTime: startTime,
        endTime: endTime }).then(res => {
        this.tableData = res.data.list
        this.total = res.data.total
        this.currentPage = res.data.pageNum
      })
    },
    onSubmit() {
      this.getUnAuditList()
    },
    downloadFile(file) {
      // if (this.imgs.indexOf(file.substring(file.lastIndexOf('.'), file.length)) !== -1) {
      //   window.open(file)
      // }
      window.open(file)
    },
    handlePass(application) {
      changeStatus({ applicationId: application.id, num: 1 }).then(res => {
        this.getUnAuditList()
      })
    },
    handleReject(application) {
      changeStatus({ applicationId: application.id, num: 2 }).then(res => {
        this.getUnAuditList()
      })
    }
  }
}
</script>

<style scoped lang="scss">
.search{
  padding-top: 20px;
  padding-left: 20px;
}
.paging {
  margin-top: 30px;
  text-align: center;
}
</style>
